package cn.t.repository.impl.order;

import cn.t.constants.ParamConstants;
import cn.t.converter.order.ReplenishmentDetailConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.ReplenishmentDetailDO;
import cn.t.persistence.entity.order.ReplenishmentDetailPO;
import cn.t.persistence.mapper.order.ReplenishmentDetailMapper;
import cn.t.repository.order.ReplenishmentDetailRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 补货订单明细表(ReplenishmentDetail)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:10
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ReplenishmentDetailRepositoryImpl extends RepositoryImpl<ReplenishmentDetailMapper, ReplenishmentDetailPO, ReplenishmentDetailConverter, ReplenishmentDetailDO> implements ReplenishmentDetailRepository {


    @Override
    public List<ReplenishmentDetailDO> findByModel(ReplenishmentDetailDO replenishmentDetailDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;

        List<ReplenishmentDetailPO> list = getWrapper(replenishmentDetailDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ReplenishmentDetailPO> getWrapper(ReplenishmentDetailDO replenishmentDetailDO) {
        LambdaQueryChainWrapper<ReplenishmentDetailPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getRsSn())) {
            wrapper.eq(ReplenishmentDetailPO::getRsSn, replenishmentDetailDO.getRsSn());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getRsSnList())) {
            wrapper.in(ReplenishmentDetailPO::getRsSn, replenishmentDetailDO.getRsSnList());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getCargowayNo())) {
            wrapper.eq(ReplenishmentDetailPO::getCargowayNo, replenishmentDetailDO.getCargowayNo());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getSkuCode())) {
            wrapper.eq(ReplenishmentDetailPO::getSkuCode, replenishmentDetailDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getSkuName())) {
            wrapper.eq(ReplenishmentDetailPO::getSkuName, replenishmentDetailDO.getSkuName());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getRsQty())) {
            wrapper.eq(ReplenishmentDetailPO::getRsQty, replenishmentDetailDO.getRsQty());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getActualRsQty())) {
            wrapper.eq(ReplenishmentDetailPO::getActualRsQty, replenishmentDetailDO.getActualRsQty());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getRsGoodsStatus())) {
            wrapper.eq(ReplenishmentDetailPO::getRsGoodsStatus, replenishmentDetailDO.getRsGoodsStatus());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getGmtModified())) {
            wrapper.between(ReplenishmentDetailPO::getGmtModified, replenishmentDetailDO.getGmtModifiedStart(), replenishmentDetailDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(replenishmentDetailDO.getGmtCreated())) {
            wrapper.between(ReplenishmentDetailPO::getGmtCreated, replenishmentDetailDO.getGmtCreatedStart(), replenishmentDetailDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(ReplenishmentDetailDO replenishmentDetailDO) {
        return getWrapper(replenishmentDetailDO).count();
    }

    @Override
    public void addBatchDetail(List<ReplenishmentDetailDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<ReplenishmentDetailPO> replenishmentDetailList = list.stream().map(converter::model2Po).collect(Collectors.toList());
        saveBatch(replenishmentDetailList, ParamConstants.EXPORTPAGEMAXSIZE);
    }
    @Override
    public void updateBatchDetail(List<ReplenishmentDetailDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<ReplenishmentDetailPO> replenishmentDetailList = list.stream().map(converter::model2Po).collect(Collectors.toList());
        updateBatchById(replenishmentDetailList, ParamConstants.EXPORTPAGEMAXSIZE);
    }
}
